package com.examples.calendar.viewer.item;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Shell;


public class CalendarControl extends Canvas implements PaintListener {
	
	Shell parentShell;
	boolean displayColumnHeader;
	boolean isLineVisible;
	List<CalendarColumn> calendarColumns = new ArrayList<CalendarColumn>();
	List<CalendarItem> calendarItems= new ArrayList<CalendarItem>();

	public CalendarControl(Shell parent, int style) {
		super(parent, style);
		this.parentShell = parent;
	}

	public void addItem(CalendarItem calendarItem) {
		calendarItems.add(calendarItem);		
	}
	
	public List<CalendarItem> getCalendarItems(){
		return calendarItems;
	}

	public void addColumn(CalendarColumn calendarColumn) {
		calendarColumns.add(calendarColumn);
	}

	public void setHeaderVisible(boolean b) {
		displayColumnHeader = b;
	}

	public void setLinesVisible(boolean b) {
		isLineVisible = b;
	}

	@Override
	public void paintControl(PaintEvent e) {
		
		final Rectangle rectangle = parentShell.getClientArea();
		
		final int totalWidth = rectangle.width;
		final int totalHeight = rectangle.height;
		final int widthOfCell = (totalWidth - 5) / 7;
		final int heightOfCell = (totalHeight - 20) / 5;
		
		GC gc = e.gc;
		gc.drawRectangle(rectangle.x, rectangle.y, totalWidth, totalHeight);
		
		if (displayColumnHeader) {
			int x = 0;
			for (CalendarColumn calendarColumn : calendarColumns) {
				gc.drawText(calendarColumn.getText(), widthOfCell * x + widthOfCell / 4 + 20, 5);
				x++;
			}
		}
		
		for (int i = 0; i <= 6; i++) {
			if (displayColumnHeader) {
				gc.drawRectangle(i * widthOfCell, 0, widthOfCell, 20);
			}
			if(isLineVisible){
				for (int j = 0; j < 5; j++) {
					gc.drawRectangle(i * widthOfCell,j * heightOfCell + 20, widthOfCell,heightOfCell);
				}
			}
		}
		
		gc.setBackground(new Color(parentShell.getDisplay(), new RGB(0, 220, 0)));
		int j = 0;
		
		for(CalendarItem item : calendarItems){
			int date = Integer.parseInt(item.getText());
			int mod = (date + 1) % (7);
			if(mod == 0){
				j++;
				gc.setBackground(new Color(parentShell.getDisplay(), new RGB(220, 100, 100)));
				if(date == 6){
					gc.drawImage(item.getImage(), 2, j * heightOfCell + 21);
					gc.drawText(" "+date+" ", 1 * widthOfCell - 13, j * heightOfCell + 21);
				}
				else
					gc.drawImage(item.getImage(), 2, j * heightOfCell + 21);
					gc.drawText(""+date, 1 * widthOfCell - 13, j * heightOfCell + 21);
			}
			else
				if(j == 0) {
					gc.drawText(" "+date+" ", (mod + 1) * widthOfCell - 13, j * heightOfCell + 21);
					gc.drawImage(item.getImage(),mod * widthOfCell + 2, j * heightOfCell + 21);
				}
				else {
					gc.setBackground(new Color(parentShell.getDisplay(), new RGB(0, 220, 0)));
					if(date == 7 || date == 8 || date == 9){
						gc.drawImage(item.getImage(), mod * widthOfCell + 2, j * heightOfCell + 21);
						gc.drawText(" "+date+" ", (mod + 1) * widthOfCell - 13, j * heightOfCell + 21);	
					}
					else{
						gc.drawImage(item.getImage(), mod * widthOfCell + 2, j * heightOfCell + 21);
						gc.drawText(""+date, (mod + 1) * widthOfCell - 13, j * heightOfCell + 21);
				}
			}
		}
	}
}